package archer.framework.common.token;

import archer.framework.cache.Cache;
import archer.framework.common.ArcherCommonModule;
import archer.framework.security.token.Token;
import archer.framework.security.token.TokenDao;
import archer.framework.utils.ValidateUtils;
import org.springframework.stereotype.Component;

/**
 * @author christ
 * @date 2016/5/31
 */
@Component
public class CacheStoreTokenDao extends Cache implements TokenDao {


    @Override
    public void create(Token token) {

        if (token == null || ValidateUtils.isEmpty(token.getId())) {
            throw new NullPointerException("token can not be null");
        }
        super.put(token.getId(), token);
    }

    @Override
    public void update(Token token) {

        if (token == null || ValidateUtils.isEmpty(token.getId())) {
            throw new NullPointerException("token can not be null");
        }
        super.put(token.getId(), token);
    }

    @Override
    public Token find(String id) {

        return (Token) super.get(id);
    }

    @Override
    public void delete(String id) {

        super.evict(id);
    }

    @Override
    protected String getRegion() {

        return ArcherCommonModule.tokenCacheRegion;
    }

    @Override
    protected String getDescription() {

        return "token缓存";
    }
}
